//
//  TL_UserDataHeaderView.m
//  WXReader
//
//  Created by TSL on 2021/5/12.
//  Copyright © 2021 Andrew. All rights reserved.
//

#import "TL_UserDataHeaderView.h"
#import "TL_ImagePicker.h"
#import "MKAliyunOSSUtils.h"

@interface TL_UserDataHeaderView()<DPImagePickerDelegate> {
    UIButton *avatarbutton;
    UIButton *tipButton;
    UIView *loadinView;
}

@end

@implementation TL_UserDataHeaderView

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, 150);
    
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    avatarbutton = [TL_UIFactory buttonWithTarget:self action:@selector(avatarEvent)];
    avatarbutton.layer.cornerRadius = 70/2;
    [self addSubview:avatarbutton];
    
    [avatarbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(70);
        make.center.mas_equalTo(self);
    }];
    
    tipButton = [TL_UIFactory buttonWithTarget:self action:@selector(avatarEvent)];
    [tipButton setBackgroundImage:[UIImage imageNamed:@"mine_photo_icon"] forState:UIControlStateNormal];
    [self addSubview:tipButton];
    
    [tipButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(20);
        make.bottom.right.mas_equalTo(avatarbutton);
    }];
}

- (void)avatarEvent {
    TL_ImagePicker *picker = [TL_ImagePicker sharedManager];
    picker.editPhoto = YES;
    picker.delegate = self;
    [picker showInController:[TL_ViewHelper getCurrentViewController]];
}

- (void)setModel:(TL_UserDataListModel *)model {
    _model = model;
    
    [avatarbutton setBackgroundImageWithURL:[NSURL URLWithString:model.desc] forState:UIControlStateNormal placeholder:HoldUserAvatar];
}
- (void)setUserHeadPic{
    
    [avatarbutton setBackgroundImageWithURL:[NSURL URLWithString:TL_UserInfoManager.userInfo.headPic] forState:UIControlStateNormal placeholder:HoldUserAvatar];
}

#pragma mark - DPImagePickerDelegate
- (void)imagePickerDidFinishPickingWithOriginalImage:(UIImage *)originalImage editedImage:(UIImage *)editedImage {
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        DISPATCH_MAIN_THREAD(^{
            loadinView = [TL_TopAlertManager showLoading:nil];
        })
    });
    
    MK_WEAK_SELF
    [[MKAliyunOSSUtils sharedInstance] uploadImage:editedImage type:CGUploadImageType_avatar block:^(NSString * _Nonnull url) {
        
        NSLog(@"上传地址：%@",url);
        DISPATCH_MAIN_THREAD(^{
            if (loadinView) {
                [loadinView removeFromSuperview];
            }
        })
        
        if (url) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [avatarbutton setImage:editedImage forState:UIControlStateNormal];
            });
            [TL_TopAlertManager showAlertWithType:TL_TopAlertTypeSuccess alertTitle:WXLocalizedString(@"修改成功")];
            
            if (weakSelf.changeAvatarBlock) {
                weakSelf.changeAvatarBlock([TL_ViewHelper fixOrientation:editedImage]);
            }
            if (weakSelf.ImgStrBlock) {
                weakSelf.ImgStrBlock(url);
            }
        }else{
            [TL_TopAlertManager showAlertWithType:TL_TopAlertTypeError alertTitle:WXLocalizedString(@"失败")];
        }
        
    }];
}

@end
